package com.gin.no_gay_alliance.database.converter;

import androidx.room.TypeConverter;

import com.gin.nga.enums.AccountStatus;
import com.gin.nga.response.field.Money;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.UUID;

/**
 * 类型转换器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/11 15:09
 **/
public class MyTypeConverter {

    @TypeConverter
    public static AccountStatus intToStatus(Integer value) {
        return value == null ? null : AccountStatus.findByValue(value);
    }

    @TypeConverter
    public static int statusToInt(AccountStatus status) {
        return status == null ? Integer.MIN_VALUE : status.value;
    }

    @TypeConverter
    public static Money intToMoney(Integer value) {
        return (value == null || value == 0) ? null : new Money(value);
    }

    @TypeConverter
    public static int moneyToInt(Money money) {
        return money == null ? 0 : money.getRaw();
    }

    @TypeConverter
    public static long zdtToLong(ZonedDateTime zdt) {
        return zdt == null ? 0 : zdt.toEpochSecond();
    }

    @TypeConverter
    public static ZonedDateTime longToZdt(long time) {
        return ZonedDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.systemDefault());
    }

    @TypeConverter
    public static UUID stringToUuid(String string) {
        if (string == null) {
            return null;
        }
        return UUID.fromString(string);
    }

    @TypeConverter
    public static String uuidToString(UUID uuid) {
        if (uuid == null) {
            return null;
        }
        return uuid.toString();
    }
}
